<?php

class GerenciaSetor{
	private $objectSetor;
	private $sqlSetor;
	private $parametro;
	private $resultSetor;
	private $conexao;
	
	public function __construct(ConectaMysql $conexao){		
		$this->conexao = $conexao;
	}
	
	public function insertSetor(Setor $objectSetor){
        $this->objectSetor = $objectSetor;
        
		$rowSetor = $this->conexao->rowCount("SELECT * FROM tb_setor WHERE SETnome = '".($this->objectSetor->getSetorNome())."'");	
		
		if ($rowSetor == 0){
			$this->conexao->executaQuery("INSERT INTO tb_setor(`SETid`,`SETnome`,`SETsigla`,`SETdata`) VALUES (
								'".($this->objectSetor->getSetorId())."',
								'".($this->objectSetor->getSetorNome())."',
								'".($this->objectSetor->getSetorSigla())."',NOW())");
			
			echo "<script language=JavaScript>";
			echo "alert('Cadastrado com sucesso');";
			echo "location.href = 'formulario.php';";
			echo "</script>";
			
		}else{
			echo "<script language=JavaScript>";
			echo "alert('Ja existe setor com esse Nome');";
			echo "location.href = 'formulario.php';";
			echo "</script>";
			
		}
		

			
	}
	
	public function updateSetor(Setor $objectSetor){
        $this->objectSetor = $objectSetor;        

		$sqlUpdate = "UPDATE tb_setor SET
						SETnome = '".($this->objectSetor->getSetorNome())."', 
						SETsigla = '".($this->objectSetor->getSetorSigla())."', 
						SETstatus = '".($this->objectSetor->getSetorStatus())."' 
					 WHERE 
						(SETid = '".($this->objectSetor->getSetorId())."')";
						
						
        $this->conexao->executaQuery($sqlUpdate);
			
		echo "<script language=JavaScript>";
		echo "alert('Alterado com sucesso');";
		echo "location.href = 'formulario.php';";
		echo "</script>";	
		
	}
	
	public function selectSetor($parametro = null, $privilegio = false){
		$this->sqlSetor = "SELECT * FROM tb_setor";
		if (isset($parametro) and ($privilegio <> '1')){
			$this->parametro = addslashes($parametro);
 			$this->sqlSetor = $this->sqlSetor." WHERE (tb_setor.SETid = '".$this->parametro."')";
		}
		
		$this->sqlSetor = $this->sqlSetor." ORDER BY tb_setor.SETsigla";
		$this->resultSetor = $this->conexao->executaQuery($this->sqlSetor);	
		return $this->resultSetor;
	}
	
	public function selectSetorTI($parametro = null){
		$this->sqlSetor = "SELECT * FROM tb_setor
						   INNER JOIN tb_ti ON
						   tb_setor.SETid = tb_ti.TI_SETid";
						   
		if (isset($parametro)){
			
			$this->parametro = addslashes($parametro);			
			$this->sqlSetor = $this->sqlSetor." WHERE (tb_setor.SETid = '".$this->parametro."')";			
		}
		
		$this->sqlSetor = $this->sqlSetor." ORDER BY tb_setor.SETsigla";
		
		$this->resultSetor = $this->conexao->executaQuery($this->sqlSetor);	
		
		return $this->resultSetor;
	}
	
	public function selectMaxId(){
		
		$this->sqlSetor = "SELECT MAX(SETid) as SETid FROM tb_setor";	
		$this->resultSetor  = $this->conexao->executaQuery($this->sqlSetor);
		return $this->resultSetor;
	}
	


}
	
?>